package com.neuedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neuedu.pojo.Item;
import com.neuedu.service.IItemService;
import com.neuedu.util.CommonResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author han
 * @since 2020-09-21
 */
@RestController
@RequestMapping("/item")
public class ItemController {
    @Resource
    IItemService iItemService;


    @GetMapping("/list")
    CommonResult list(Item item) {
        return CommonResult.success(iItemService.list(item));
    }


    @PostMapping("/add")
    CommonResult add(Item item) {
        item.setActive(1);
        if(iItemService.add(item))
            return CommonResult.success("立项成功");
        else
            return CommonResult.failed("此项目已经存在");
    }
    @GetMapping("/getById")
    CommonResult getById(Integer id) {
        return CommonResult.success(iItemService.getById(id));
    }
    @PostMapping("/update")
    CommonResult update(Item item) {
        return CommonResult.success(iItemService.updateById(item));
    }
    @PostMapping("/del")
    CommonResult del(Item item) {
        item.setActive(0);
        return CommonResult.success(iItemService.updateById(item));
    }
    @PostMapping("/batchdel")
    CommonResult batchdel(Integer[] ids) {
        return CommonResult.success(iItemService.batchdel(ids));
    }
    @PostMapping("/back")
    CommonResult back(Item item) {
        item.setActive(1);
        return CommonResult.success(iItemService.updateById(item));
    }
    @GetMapping("/getByType")
    CommonResult getByType(Integer typeId){
        QueryWrapper<Item> qu=new QueryWrapper<>();
        qu.eq("type_id",typeId);
        qu.eq("active",1);
        return CommonResult.success(iItemService.list(qu));
    }
}
